{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ff19f386",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "import copy\n",
    "import  traceback\n",
    "import shutil\n",
    "import random\n",
    "\n",
    "import numpy as np  # linear algebra\n",
    "import pydicom\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n",
    "from pydicom.uid import UID\n",
    "from PIL import Image\n",
    "from tqdm import tqdm\n",
    "import openpyxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b6d635d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_scan(path):\n",
    "    slices = []\n",
    "    for s in os.listdir(path):\n",
    "        if not s.endswith('.dcm'):\n",
    "            continue\n",
    "        sl = pydicom.dcmread(os.path.join(path, s))\n",
    "        try:\n",
    "            sl_p = sl.pixel_array\n",
    "        except AttributeError:\n",
    "            traceback.print_exc()\n",
    "            print(f'\\tDelete {os.path.join(path, s)}')\n",
    "            os.remove(os.path.join(path, s))\n",
    "        else:\n",
    "            slices.append(sl)\n",
    "    slices.sort(key=lambda x: float(x.InstanceNumber))\n",
    "    return slices, slices[0].SliceThickness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3baa3cae",
   "metadata": {},
   "outputs": [],
   "source": [
    "lower_b, upper_b = -100, 500"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "467c2c97",
   "metadata": {},
   "outputs": [],
   "source": [
    "root = '/nfs3-p1/zsxm/dataset/2021-9-17-negative'\n",
    "not_2_list = []\n",
    "for patient in sorted(os.listdir(root)):\n",
    "    patient = os.path.join(root, patient)\n",
    "    if os.path.isfile(patient):\n",
    "        continue\n",
    "    if len(os.listdir(patient)) > 2:\n",
    "        not_2_list.append(patient)\n",
    "        continue\n",
    "    ct, ct_thickness = load_scan(os.path.join(patient, '1'))\n",
    "    try:\n",
    "        cta, cta_thickness = load_scan(os.path.join(patient, '2'))\n",
    "    except FileNotFoundError:\n",
    "        print(patient, 'not have 2')\n",
    "        continue\n",
    "    if ct_thickness % cta_thickness != 0:\n",
    "        print(patient, ct_thickness, cta_thickness)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c0879c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "for patient in not_2_list:\n",
    "    print(patient)\n",
    "print(len(not_2_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55648e81",
   "metadata": {},
   "outputs": [],
   "source": [
    "root = '/nfs3-p1/zsxm/dataset/2021-10-19-imh'\n",
    "wb = openpyxl.load_workbook(os.path.join(root, 'label_ori.xlsx'))\n",
    "sheet = wb['Sheet1']\n",
    "for patient in sorted(os.listdir(root)):\n",
    "    name = patient.split('-')[0]\n",
    "    patient = os.path.join(root, patient)\n",
    "    if os.path.isfile(patient):\n",
    "        continue\n",
    "    for row in sheet.iter_rows():\n",
    "        if row[0].value == name:\n",
    "            ct_label = row[3].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "            cta_label = row[4].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "            break\n",
    "    print(name, ct_label, cta_label)\n",
    "    ctl, ctal = ct_label.split('-'), cta_label.split('-')\n",
    "    assert len(ctl) == len(ctal), name\n",
    "    ct_start, ct_end, cta_start, cta_end = 10000, -1, 10000, -1\n",
    "    ct_min_i, ct_max_i, cta_min_i, cta_max_i = -1, -1, -1, -1\n",
    "    for i in range(len(ctl)):\n",
    "        if int(ctl[i])-1 < ct_start:\n",
    "            ct_start = int(ctl[i])-1\n",
    "            ct_min_i = i\n",
    "        if int(ctl[i])-1 > ct_end:\n",
    "            ct_end = int(ctl[i])-1\n",
    "            ct_max_i = i\n",
    "        if int(ctal[i])-1 < cta_start:\n",
    "            cta_start = int(ctal[i])-1\n",
    "            cta_min_i = i\n",
    "        if int(ctal[i])-1 > cta_end:\n",
    "            cta_end = int(ctal[i])-1\n",
    "            cta_max_i = i\n",
    "        assert ct_min_i == cta_min_i, f'{len(ct)}  {len(cta)}'\n",
    "        assert ct_max_i == cta_max_i, f'{len(ct)}  {len(cta)}'\n",
    "    m, n = ct_end - ct_start, cta_end - cta_start\n",
    "    assert m>0 and n>0\n",
    "    print(m, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c85b202",
   "metadata": {},
   "outputs": [],
   "source": [
    "def combine_image0(root):    \n",
    "    wb = openpyxl.load_workbook(os.path.join(root, 'label_ori.xlsx'))\n",
    "    sheet = wb['Sheet1']\n",
    "    size_not_match = set()\n",
    "    for patient in sorted(os.listdir(root)):\n",
    "        name = patient.split('-')[0]\n",
    "        folder_name = patient\n",
    "        patient = os.path.join(root, patient)\n",
    "        if os.path.isfile(patient):\n",
    "            continue\n",
    "        ct, ct_thickness = load_scan(os.path.join(patient, '1'))\n",
    "        cta, cta_thickness = load_scan(os.path.join(patient, '2'))\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == name:\n",
    "                ct_label = row[3].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "                cta_label = row[4].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "                break\n",
    "        print(name, ct_label, cta_label)\n",
    "        ctl, ctal = ct_label.split('-'), cta_label.split('-')\n",
    "        assert len(ctl) == len(ctal), name\n",
    "        ct_cta_table = [-len(cta)] * len(ct)\n",
    "        if ct_thickness % cta_thickness == 0:\n",
    "            times = int(ct_thickness // cta_thickness)\n",
    "            ct_pivot, cta_pivot = int(ctl[0])-1, int(ctal[0])-1\n",
    "            for i in range(len(ct)):\n",
    "                ct_cta_table[i] = min(max(cta_pivot + (i - ct_pivot) * times, 0), len(cta)-1)\n",
    "        else:\n",
    "            ct_start, ct_end, cta_start, cta_end = int(ctl[0])-1, int(ctl[1])-1, int(ctal[0])-1, int(ctal[1])-1\n",
    "            if ct_start > ct_end:\n",
    "                ct_start, ct_end, cta_start, cta_end = ct_end, ct_start, cta_end, cta_start\n",
    "            m, n = ct_end - ct_start, cta_end - cta_start\n",
    "            ot, q = [], 0\n",
    "            for p in range(m):\n",
    "                min_dis = abs(q/n-p/m)\n",
    "                while q < n:\n",
    "                    q += 1\n",
    "                    if abs(q/n-p/m) < min_dis:\n",
    "                        min_dis = abs(q/n-p/m)\n",
    "                    else:\n",
    "                        q -= 1\n",
    "                        ot.append(q)\n",
    "                        break\n",
    "            print(ot)\n",
    "            assert len(ot) == m, name\n",
    "            for i in range(len(ct)):\n",
    "                ct_cta_table[i] = min(max(cta_start + (i-ct_start)//m*n + ot[(i-ct_start)%m], 0), len(cta)-1)\n",
    "        #print(ct_cta_table)\n",
    "        assert min(ct_cta_table) >=0 and max(ct_cta_table) < len(cta), f'{name}:{min(ct_cta_table)},{max(ct_cta_table)},{len(cta)}'\n",
    "        image_path = os.path.join(patient, f'combine_images_{lower_b}_{upper_b}')\n",
    "        if os.path.exists(image_path):\n",
    "             shutil.rmtree(image_path)\n",
    "        os.mkdir(image_path)\n",
    "        for i in range(len(ct)):\n",
    "            ct_img = ct[i].pixel_array.astype(np.int16)\n",
    "            intercept = ct[i].RescaleIntercept\n",
    "            slope = ct[i].RescaleSlope\n",
    "            if slope != 1:\n",
    "                ct_img = (slope * ct_img.astype(np.float64)).astype(np.int16)\n",
    "            ct_img += np.int16(intercept)\n",
    "            ct_img = np.clip(ct_img, lower_b, upper_b)\n",
    "            ct_img = ((ct_img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "\n",
    "            cta_img = cta[ct_cta_table[i]].pixel_array.astype(np.int16)\n",
    "            intercept = cta[ct_cta_table[i]].RescaleIntercept\n",
    "            slope = cta[ct_cta_table[i]].RescaleSlope\n",
    "            if slope != 1:\n",
    "                cta_img = (slope * cta_img.astype(np.float64)).astype(np.int16)\n",
    "            cta_img += np.int16(intercept)\n",
    "            cta_img = np.clip(cta_img, lower_b, upper_b)\n",
    "            cta_img = ((cta_img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "\n",
    "            if ct_img.shape[0] < cta_img.shape[0]:\n",
    "                crop_start = int(round((cta_img.shape[0]-ct_img.shape[0])/2.))\n",
    "                cta_img = cta_img[crop_start: crop_start+ct_img.shape[0]]\n",
    "            if ct_img.shape[1] < cta_img.shape[1]:\n",
    "                crop_start = int(round((cta_img.shape[1]-ct_img.shape[1])/2.))\n",
    "                cta_img = cta_img[:, crop_start: crop_start+ct_img.shape[1]]\n",
    "            if ct_img.shape[0] > cta_img.shape[0] or ct_img.shape[1] > cta_img.shape[1]:\n",
    "                size_not_match.add(patient)\n",
    "                break\n",
    "\n",
    "            img = np.concatenate((ct_img, cta_img), axis=1)\n",
    "            img = Image.fromarray(img)\n",
    "            img.save(os.path.join(image_path, f'{folder_name}_{i:04d}_{ct_cta_table[i]:04d}.png'))\n",
    "            \n",
    "combine_image0('/nfs3-p1/zsxm/dataset/2021-10-19-imh')\n",
    "combine_image0('/nfs3-p1/zsxm/dataset/2021-10-19-pau')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5185fda",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def combine_image(root):    \n",
    "    wb = openpyxl.load_workbook(os.path.join(root, 'label_ori.xlsx'))\n",
    "    sheet = wb['Sheet1']\n",
    "    size_not_match = set()\n",
    "    for patient in sorted(os.listdir(root)):\n",
    "        name = patient.split('-')[0]\n",
    "        folder_name = patient\n",
    "        patient = os.path.join(root, patient)\n",
    "        if os.path.isfile(patient):\n",
    "            continue\n",
    "        ct, ct_thickness = load_scan(os.path.join(patient, '1'))\n",
    "        cta, cta_thickness = load_scan(os.path.join(patient, '2'))\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == name:\n",
    "                ct_label = row[3].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "                cta_label = row[4].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "                break\n",
    "        print(name, ct_label, cta_label)\n",
    "        ctl, ctal = ct_label.split('-'), cta_label.split('-')\n",
    "        assert len(ctl) == len(ctal), name\n",
    "        ct_cta_table = [-len(cta)] * len(ct)\n",
    "        ct_start, ct_end, cta_start, cta_end = len(ct), -1, len(cta), -1\n",
    "        ct_min_i, ct_max_i, cta_min_i, cta_max_i = -1, -1, -1, -1\n",
    "        for i in range(len(ctl)):\n",
    "            if int(ctl[i])-1 < ct_start:\n",
    "                ct_start = int(ctl[i])-1\n",
    "                ct_min_i = i\n",
    "            if int(ctl[i])-1 > ct_end:\n",
    "                ct_end = int(ctl[i])-1\n",
    "                ct_max_i = i\n",
    "            if int(ctal[i])-1 < cta_start:\n",
    "                cta_start = int(ctal[i])-1\n",
    "                cta_min_i = i\n",
    "            if int(ctal[i])-1 > cta_end:\n",
    "                cta_end = int(ctal[i])-1\n",
    "                cta_max_i = i\n",
    "            assert ct_min_i == cta_min_i and ct_max_i == cta_max_i\n",
    "        m, n = ct_end - ct_start, cta_end - cta_start\n",
    "        assert m > 0 and n > 0\n",
    "        ot, q = [], 0\n",
    "        for p in range(m):\n",
    "            min_dis = abs(q/n-p/m)\n",
    "            while q < n:\n",
    "                q += 1\n",
    "                if abs(q/n-p/m) < min_dis:\n",
    "                    min_dis = abs(q/n-p/m)\n",
    "                else:\n",
    "                    q -= 1\n",
    "                    ot.append(q)\n",
    "                    break\n",
    "        #print(ot)\n",
    "        assert len(ot) == m, name\n",
    "        for i in range(len(ct)):\n",
    "            ct_cta_table[i] = min(max(cta_start + (i-ct_start)//m*n + ot[(i-ct_start)%m], 0), len(cta)-1)\n",
    "        #print(ct_cta_table)\n",
    "        assert min(ct_cta_table) >=0 and max(ct_cta_table) < len(cta), f'{name}:{min(ct_cta_table)},{max(ct_cta_table)},{len(cta)}'\n",
    "        image_path = os.path.join(patient, f'combine_images_{lower_b}_{upper_b}')\n",
    "        if os.path.exists(image_path):\n",
    "             shutil.rmtree(image_path)\n",
    "        os.mkdir(image_path)\n",
    "        for i in range(len(ct)):\n",
    "            ct_img = ct[i].pixel_array.astype(np.int16)\n",
    "            intercept = ct[i].RescaleIntercept\n",
    "            slope = ct[i].RescaleSlope\n",
    "            if slope != 1:\n",
    "                ct_img = (slope * ct_img.astype(np.float64)).astype(np.int16)\n",
    "            ct_img += np.int16(intercept)\n",
    "            ct_img = np.clip(ct_img, lower_b, upper_b)\n",
    "            ct_img = ((ct_img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "\n",
    "            cta_img = cta[ct_cta_table[i]].pixel_array.astype(np.int16)\n",
    "            intercept = cta[ct_cta_table[i]].RescaleIntercept\n",
    "            slope = cta[ct_cta_table[i]].RescaleSlope\n",
    "            if slope != 1:\n",
    "                cta_img = (slope * cta_img.astype(np.float64)).astype(np.int16)\n",
    "            cta_img += np.int16(intercept)\n",
    "            cta_img = np.clip(cta_img, lower_b, upper_b)\n",
    "            cta_img = ((cta_img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "\n",
    "            if ct_img.shape[0] < cta_img.shape[0]:\n",
    "                crop_start = int(round((cta_img.shape[0]-ct_img.shape[0])/2.))\n",
    "                cta_img = cta_img[crop_start: crop_start+ct_img.shape[0]]\n",
    "            if ct_img.shape[1] < cta_img.shape[1]:\n",
    "                crop_start = int(round((cta_img.shape[1]-ct_img.shape[1])/2.))\n",
    "                cta_img = cta_img[:, crop_start: crop_start+ct_img.shape[1]]\n",
    "            if ct_img.shape[0] > cta_img.shape[0] or ct_img.shape[1] > cta_img.shape[1]:\n",
    "                size_not_match.add(patient)\n",
    "                break\n",
    "\n",
    "            img = np.concatenate((ct_img, cta_img), axis=1)\n",
    "            img = Image.fromarray(img)\n",
    "            img.save(os.path.join(image_path, f'{folder_name}_{i+1:04d}_{ct_cta_table[i]+1:04d}.png'))\n",
    "            \n",
    "combine_image('/nfs3-p1/zsxm/dataset/2021-10-19-imh')\n",
    "combine_image('/nfs3-p1/zsxm/dataset/2021-10-19-pau')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2b63158",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def move_together(root, dist):\n",
    "    os.makedirs(dist, exist_ok=True)\n",
    "    for patient in sorted(os.listdir(root)):\n",
    "        if os.path.isfile(os.path.join(root, patient)):\n",
    "            continue\n",
    "        print(f'Processing {patient}')\n",
    "        img_path = os.path.join(root, patient, f'combine_images_{lower_b}_{upper_b}')\n",
    "        shutil.copytree(img_path, os.path.join(dist, patient))\n",
    "\n",
    "move_together('/nfs3-p1/zsxm/dataset/2021-10-19-imh', '/nfs3-p1/zsxm/dataset/aorta_combine/imh')\n",
    "move_together('/nfs3-p1/zsxm/dataset/2021-10-19-pau', '/nfs3-p1/zsxm/dataset/aorta_combine/pau')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17b6beb5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5d294d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "positive_folders = ['/nfs3-p2/zsxm/dataset/2021-9-8/','/nfs3-p2/zsxm/dataset/2021-9-13/','/nfs3-p2/zsxm/dataset/2021-9-19/','/nfs3-p2/zsxm/dataset/2021-9-28/']\n",
    "patient_list = []\n",
    "for folder in positive_folders:\n",
    "    for patient in sorted(os.listdir(folder)):\n",
    "        patient = os.path.join(folder, patient)\n",
    "        if os.path.isdir(patient):\n",
    "            patient_list.append(patient)\n",
    "            \n",
    "            \n",
    "            \n",
    "selected_list = ['baochunsheng-J-20-82', 'caiweiguang-J-Im35-152', 'chenbo-S-Im24-45-J-Im24-97', 'chenfujun-J-Im43-100', 'chengshizheng-J-Im30-145', 'chengyonghui-J-17-81', 'chenjiandong-S-19-28-J-19-91', 'chenping-J-Im23-88', 'chensiqi-S-17-28-J-17-69', 'chugentang-J-Im19-80', 'fangchunfeng-J-16-48', 'fanghongwei-J-22-71', 'guanxujun-S-Im25-35-J-Im25-87', 'guoheying-J-Im32-127', 'guquankang-J-Im18-69', 'huangdebing-S-Im40-48-J-Im33-138', 'huanghai-S-Im19-29-J-Im19-80', 'huangsuyue-J-Im21-41', 'huangxihong-J-Im27-136', 'huangyong-J-Im18-127', 'huangyongliang-J-Im16-81', 'jiadeen-J-Im34-148', 'jihongke-S-Im20-24-J-Im20-86', 'laiguizhen-J-Im38-121', 'laixuexiang-J-Im33-148', 'lanshaomei-S-Im20-31-J-Im20-79', 'leixiaoying-S-Im18-26-J-18-71', 'lihuanhuan-S-Im20-28-J-Im20-59', 'liubihai-S-23-26-J-23-93', 'liyehua-S-Im30-47-J-Im30-131', 'louyidong-S-Im38-58-J-Im38-133', 'luojun-J-Im22-81', 'maliwei-S-Im30-48-J-Im30-101', 'pengzhengjiang-J-Im38-160', 'qumin-S-Im22-35-J-Im22-28', 'ruweiping-S-Im52-63-J-Im52-94', 'shaoyefeng-S-Im34-43-J-Im34-48', 'shenglina-J-Im21-65', 'shenqi-J-Im24-72', 'tangabiao-S-Im41-49-J-Im41-138', 'wangjiangwei-J-Im37-143', 'wangxiaofu-S-19-29-J-19-65', 'wangyonghui-S-Im18-27-J-Im18-84', 'wangyuefeng-S-Im34-38-J-Im34-144', 'wangzhengjin-J-16-82', 'wenyongguo-J-Im23-121', 'wuyueming-J-Im36-138', 'xiaminsong-S-Im40-51-J-Im40-139', 'xiashubiao-J-24-88', 'xuhangying-J-25-63', 'xuyaofeng-J-Im24-119', 'xuzhaofang-S-21-26-J-21-86', 'yangmin-J-Im37-150', 'yangxuehua-J-Im23-79', 'yangzhanxiang-J-Im31-145', 'yepeng-J-Im26-125', 'yexianchang-S-17-30-J-17-74', 'yexiyou-J-Im35-139', 'yujiada-J-Im21-48', 'zhangguangming-J-21-138', 'zhanghaitao-S-Im25-49-J-Im25-145', 'zhangwei-S-20-26-J-20-61', 'zhangyunqin-S-Im14-25-J-Im14-64', 'zhaowenxian-J-Im43-111', 'zhengguozheng-J-18-81', 'zhengjiyou-J-Im28-136', 'zhoufeng-S-Im35-40-J-Im35-147', 'zhouliang-J-Im47-123', 'zhuseng-S-Im24-31-J-Im24-105', 'zhuxutao-J-Im23-78', 'zhuyongfu-J-Im22-80', 'zhuyuejin-J-Im21-72', 'zongminghui-S-Im18-25-J-Im18-81']+['chenggang-J-Im18-81', 'chenjufa-J-16-66', 'chenjuli-J-Im18-49', 'chenjun-J-Im33-137', 'chenlili-S-Im18-24-J-Im18-74', 'chiyanfei-J-Im20-83', 'daizuokou-J-Im19-74', 'ganxiaobin-J-23-51', 'guojianfu-J-Im32-100', 'hongzhimin-S-Im30-39-J-30-144', 'huanglijun-J-24-98', 'huleijun-J-Im25-141', 'jilixian-J-Im34-108', 'jinchongfei-J-22-92', 'lijianming-J-Im32-124', 'lingenqiang-S-Im30-39', 'linjiaxiang-J-21-72', 'liuyunfei-J-Im40-95', 'loulinhua-S-Im27-30', 'lufeng-J-Im28-103', 'luzhiping-S-Im25-35-J-Im25-54', 'panzhangsong-J-Im26-91', 'qianfuying-J-Im20-87', 'qiaowei-S-Im18-30-J-Im18-82', 'shanghongjun-J-22-84', 'shaoxiulan-J-Im21-25', 'shengwenping-S-Im37-46-J-Im37-116', 'shenliqiang-J-Im29-137', 'shenxuefu-S-Im33-40-J-Im33-101', 'sishouzhong-J-Im25-137', 'wangqing-J-35-102', 'wutonggen-S-Im22-29-J-Im22-82', 'wuwanglong-J-Im31-125', 'wuxiangyang-S-Im18-25', 'xiafangzhou-S-18-25-J-18-80', 'xuhong-S-Im23-38-J-Im23-40', 'xushichao-J-Im35-145', 'yangbingshui-S-Im25-33-J-Im25-56', 'yanjuanfeng-J-Im30-32', 'yaocaiming-J-Im26-88', 'yaojianmin-S-Im20-29-J-Im20-86', 'yesanghua-J-Im41-138', 'yewenyi-J-Im36-130', 'yingguoliang-J-Im55-130', 'yingjianquan-S-Im12-14-J-Im12-36', 'yingmeiqi-S-Im22-25-J-Im16-36', 'yintianxing-J-Im24-38', 'yuanlinyue-J-Im27-120', 'yuguiying-S-16-22-J-16-77', 'yuhongliang-S-Im26-37-J-Im26-105', 'yujiazhen-S-Im19-27-J-Im19-77', 'zhangboqian-S-Im22-32-J-Im22-88', 'zhangfuyang-S-Im15-20-J-Im15-87', 'zhangjian-J-26-89', 'zhanglimin-J-Im40-149', 'zhongxuefang-S-Im19-34-J-Im19-72', 'zhoubozhong-J-Im29-131', 'zhoumingfang-J-Im36-41', 'zhousuhua-J-Im25-96', 'zhuhancheng-S-Im27-36-J-Im27-86']\n",
    "processed_list = []\n",
    "for patient in patient_list:\n",
    "    if patient.split('/')[-1] in selected_list:\n",
    "        print(patient)\n",
    "        processed_list.append(patient)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7268cd50",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "642a46c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bairuixin 29-46 39-68\n",
      "J-29-46 J-39-68\n",
      "baochangmu 13-77 63-381\n",
      "J-18-77 J-88-381\n",
      "baodezhong 41-44 40-43\n",
      "J-41-44 J-40-43\n",
      "chenbingrong 61-96 182-287\n",
      "J-61-96 J-182-287\n",
      "chenfuding 29-37 143-183\n",
      "J-29-37 J-143-183\n",
      "chenmazhang 18-22 53-62\n",
      "J-18-22 J-53-62\n",
      "chenmingbao 34-96 101-287\n",
      "J-38-96 J-113-287\n",
      "chenrongqin 24-100 71-300\n",
      "J-32-100 J-95-300\n",
      "chensimeng 27-33 81-98\n",
      "J-31-33 J-92-98\n",
      "dongyoufa 48-87 142-259\n",
      "J-48-87 J-142-259\n",
      "fanggaoshen 82-87 325-345\n",
      "J-82-87 J-325-345\n",
      "fangpingan 29-34 86-101\n",
      "J-29-34 J-86-101\n",
      "feiliangming 19-41 19-41\n",
      "J-23-41 J-23-41\n",
      "gaohuarong 51-109 152-326\n",
      "J-51-109 J-152-326\n",
      "gaozhihua 82-94 408-468\n",
      "J-82-94 J-408-468\n",
      "gebingzhao 38-40 189-197\n",
      "J-38-40 J-189-197\n",
      "guxin 80-88 80-88\n",
      "J-80-88 J-80-88\n",
      "heqiang 36-45 106-133\n",
      "J-36-45 J-106-133\n",
      "heshimo 44-46 131-137\n",
      "J-44-46 J-131-137\n",
      "heshufang 26-30 77-89\n",
      "J-28-30 J-83-89\n",
      "huajinyuan 58-82 173-246\n",
      "J-58-82 J-173-246\n",
      "huanggentian 121-132 362-395\n",
      "J-121-132\n",
      " J-362-395\n",
      "huangyuanzhong 18-103 52-309\n",
      "J-24-103 J-70-309\n",
      "huboan 25-131 74-391\n",
      "J-33-131 J-98-391\n",
      "huxuegen 35-37-60-74 104-110-179-221\n",
      "S-37-37-J-60-74 S-110-110-J-179-221\n",
      "jiangxianghong 132-146 394-438\n",
      "J-132-146 J-394-438\n",
      "jinguozhong 29-116 87-348\n",
      "J-37-116 J-111-348\n",
      "lifurong 54-62 161-185\n",
      "J-54-62 J-161-185\n",
      "linjinmu 36-120 106-358\n",
      "J-36-120 J-106-358\n",
      "linxiulian 18-17 18-17\n",
      "S-17-18-J-17-17 S-17-18-J-17-17\n",
      "linyunfu 27-29 27-29\n",
      "S-27-29 S-27-29\n",
      "liushurong 72-78 215-232\n",
      "J-72-78 J-215-232\n",
      "liuyongliang 122-131 365-392\n",
      "J-122-131 J-365-392\n",
      "liyi 23-42 23-42\n",
      "J-23-42 J-23-42\n",
      "liyonghong 48-54 144-161\n",
      "J-48-54 J-144-161\n",
      "liyuezhi 53-60 115-129\n",
      "J-53-60 J-115-129\n",
      "lizuojing 30-38 85-108\n",
      "J-35-38 J-99-108\n",
      "magengqiu 47-55 47-55\n",
      "S-50-55 S-50-55\n",
      "maoshangxin 42-81 42-81\n",
      "J-44-81 J-44-81\n",
      "meichunming 116-119 347-355\n",
      "J-116-119 J-347-355\n",
      "miaohengtai 39-135 116-404\n",
      "S-36-39-J-36-135 S-107-116-J-107-404\n",
      "nigengrong 19-32 38-63\n",
      "J-25-32 J-50-63\n",
      "qiaozhiyin 55-65-117-142 164-194-350-425\n",
      "J-55-65-J-117-142 J-164-194-J-350-425\n",
      "qiuguozhang 68-77 203-230\n",
      "J-68-77 J-203-230\n",
      "qiujinrong 24-57-78-125-137-158 27-60-81-128-140-161\n",
      "J-33-57-J-78-125-J-137-158 J-36-60-J-81-128-J-140-161\n",
      "qiumeijun 45-52 134-152\n",
      "J-45-52 J-134-152\n",
      "quanjianzhong 28-35 83-104\n",
      "S-32-35 S-95-104\n",
      "rongjiliang 27-43 82-131\n",
      "J-31-43 J-94-131\n",
      "shengyueqin 17-41 83-205\n",
      "J-21-41 J-103-205\n",
      "songguoan 30-40 29-39\n",
      "J-30-40 J-29-39\n",
      "suzhijing 51-55 51-55\n",
      "J-51-55 J-51-55\n",
      "tuchenghe 90-123 273-372\n",
      "J-90-123 J-273-372\n",
      "tuhuadong 19-30 19-30\n",
      "J-22-30 J-22-30\n",
      "wangbaosheng 57-87 57-87\n",
      "J-57-87 J-57-87\n",
      "wangdeqian 26-123 77-368\n",
      "J-36-123 J-107-368\n",
      "wangdequan 21-121 61-362\n",
      "J-25-121 J-73-362\n",
      "wangguoping 25-30-31-94 38-45-47-141\n",
      "J-31-94 J-47-141\n",
      "wangshiyao 46-139 137-416\n",
      "J-46-139 J-137-416\n",
      "wangyeping 69-78 206-233\n",
      "J-69-78 J-206-233\n",
      "wugenbao 15-78 29-156\n",
      "J-16-78 J-31-156\n",
      "xiahongsheng 24-86 118-428\n",
      "J-24-86 J-118-428\n",
      "xiangbaiquan 29-100 86-299\n",
      "J-38-100 J-113-299\n",
      "xingwenqu 20-53 20-53\n",
      "J-24-53 J-24-53\n",
      "xuping 27-79 78-234\n",
      "J-35-79 J-102-234\n",
      "xuwenxiang 50-67 50-67\n",
      "J-50-67 J-50-67\n",
      "yingguocheng 48-54 143-162\n",
      "J-48-54 J-143-162\n",
      "yujianshun 17-20 33-39\n",
      "S-19-20 S-37-39\n",
      "yuxingguan 22-30 65-88\n",
      "J-25-30 J-74-88\n",
      "zengchenhong 80-86 239-257\n",
      "J-80-86 J-239-257\n",
      "zhangshaohong 16-20 16-20\n",
      "S-17-20 S-17-20\n",
      "zhengaizhu 21-29-52-56 62-86-156-167\n",
      "S-26-29-J-52-56 S-77-86-J-155-167\n",
      "zhengxiusheng 22-27 19-24\n",
      "S-26-27 S-23-24\n",
      "zhongjianhong 22-63 65-187\n",
      "J-29-63 J-86-187\n",
      "zhongzhongnan 22-37 22-37\n",
      "J-22-37 J-22-37\n",
      "zhougensheng 34-42 101-123\n",
      "S-39-42 S-115-123\n",
      "zhourongcheng 80-93 238-277\n",
      "J-80-93 J-238-277\n",
      "zhouxincang 34-97 34-97\n",
      "J-40-97 J-40-97\n",
      "zhujianping 26-34 77-102\n",
      "J-31-34 J-93-102\n"
     ]
    }
   ],
   "source": [
    "def change_label(root):    \n",
    "    wb = openpyxl.load_workbook(os.path.join(root, 'label_ori.xlsx'))\n",
    "    sheet = wb['Sheet1']\n",
    "    wb2_path = os.path.join(root, 'label.xlsx')\n",
    "    wb2 = openpyxl.load_workbook(wb2_path)\n",
    "    sheet2 = wb2['Sheet1']\n",
    "    for patient in sorted(os.listdir(root)):\n",
    "        name = patient.split('-')[0]\n",
    "        folder_name = patient\n",
    "        patient = os.path.join(root, patient)\n",
    "        if os.path.isfile(patient):\n",
    "            continue\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == name:\n",
    "                ct_label = row[3].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "                cta_label = row[4].value.replace('\\n', ' ').replace(' ', '-').upper().replace('S', '').replace('J', '').replace('G', '')\n",
    "                break\n",
    "        print(name, ct_label, cta_label)\n",
    "        ctl, ctal = ct_label.split('-'), cta_label.split('-')\n",
    "        assert len(ctl) == len(ctal), f'{name}, {ctl}, {ctal}'\n",
    "        len_ct = len(os.listdir(os.path.join(patient, '1')))\n",
    "        len_cta = len(os.listdir(os.path.join(patient, '2')))\n",
    "        ct_cta_table = [-len_cta] * len_ct\n",
    "        ct_start, ct_end, cta_start, cta_end = len_ct, -1, len_cta, -1\n",
    "        ct_min_i, ct_max_i, cta_min_i, cta_max_i = -1, -1, -1, -1\n",
    "        for i in range(len(ctl)):\n",
    "            if int(ctl[i])-1 < ct_start:\n",
    "                ct_start = int(ctl[i])-1\n",
    "                ct_min_i = i\n",
    "            if int(ctl[i])-1 > ct_end:\n",
    "                ct_end = int(ctl[i])-1\n",
    "                ct_max_i = i\n",
    "            if int(ctal[i])-1 < cta_start:\n",
    "                cta_start = int(ctal[i])-1\n",
    "                cta_min_i = i\n",
    "            if int(ctal[i])-1 > cta_end:\n",
    "                cta_end = int(ctal[i])-1\n",
    "                cta_max_i = i\n",
    "            assert ct_min_i == cta_min_i and ct_max_i == cta_max_i\n",
    "        m, n = ct_end - ct_start, cta_end - cta_start\n",
    "        assert m > 0 and n > 0\n",
    "        ot, q = [], 0\n",
    "        for p in range(m):\n",
    "            min_dis = abs(q/n-p/m)\n",
    "            while q < n:\n",
    "                q += 1\n",
    "                if abs(q/n-p/m) < min_dis:\n",
    "                    min_dis = abs(q/n-p/m)\n",
    "                else:\n",
    "                    q -= 1\n",
    "                    ot.append(q)\n",
    "                    break\n",
    "        #print(ot)\n",
    "        assert len(ot) == m, name\n",
    "        for i in range(len(ct_cta_table)):\n",
    "            ct_cta_table[i] = min(max(cta_start + (i-ct_start)//m*n + ot[(i-ct_start)%m], 0), len_cta-1)\n",
    "        #print(ct_cta_table)\n",
    "        assert min(ct_cta_table) >=0 and max(ct_cta_table) < len_cta, f'{name}:{min(ct_cta_table)},{max(ct_cta_table)},{len_cta}'\n",
    "        \n",
    "        for row in sheet2.iter_rows():\n",
    "            if row[0].value == name:\n",
    "                lbs = row[3].value.split('-')\n",
    "                albs = ''\n",
    "                for i, lb in enumerate(lbs):\n",
    "                    if i != 0:\n",
    "                        albs += '-'\n",
    "                    try:\n",
    "                        albs += str(ct_cta_table[int(lb)-1]+1)\n",
    "                    except ValueError:\n",
    "                        albs += lb\n",
    "                print(row[3].value, albs)\n",
    "                row[4].value = albs\n",
    "                break\n",
    "    wb2.save(wb2_path)\n",
    "                \n",
    "change_label('/nfs3-p2/zsxm/dataset/2021-10-19-pau')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f59ed66",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
